VERSION 0.7
FROM node:18-bullseye
WORKDIR /konfig-dash

# TODO: Once we figure out how to better package/extract konfig-cli from konfig-dash, move this to
#       generator/konfig-integration-tests/Earthfile as the base image for +integration-tests
konfig-test-dependencies:
  ENV PYTHONDONTWRITEBYTECODE=1 # Don't write .pyc files, which are not needed in a container
  ENV PIP_DEFAULT_TIMEOUT=100 # Increase pip timeout to avoid timeouts when installing python packages
  ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true # Avoids error when running dotnet
  # To install php, first add SURY repository (php repository for debian)
  RUN apt-get update && apt-get install --no-install-recommends -y \
      software-properties-common ca-certificates lsb-release apt-transport-https
  RUN sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list' && \
      wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -
  RUN apt-get update && apt-get install --no-install-recommends -y \
      python3 python3-pip \ # python
      openjdk-11-jdk maven \ # java
      golang \ # go
      ruby-full \ # ruby
      php8.2 php8.2-curl php8.2-dom php8.2-mbstring php8.2-tokenizer php8.2-xml php8.2-xmlwriter \ # php
      lsof \ # used by konfig test
      openssl # used by prisma
  RUN gem install bundler
  RUN curl -s https://packagecloud.io/install/repositories/swift-arm/release/script.deb.sh | bash
  RUN apt install -y swiftlang
  RUN swift --version
  RUN npm install -g pnpm
  RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # used for php
  RUN curl -Lo dotnet-install.sh https://dot.net/v1/dotnet-install.sh && \
      chmod +x ./dotnet-install.sh && \
      ./dotnet-install.sh --channel 7.0 && \
      ./dotnet-install.sh --channel 5.0 && \
      ./dotnet-install.sh --channel 3.1
  ENV PATH=$PATH:/root/.dotnet
  RUN pip3 install --upgrade pip && pip3 install poetry
  RUN apt-get clean && rm -rf /var/lib/apt/lists/* # Clean up

build-konfig-dash:
  FROM +konfig-test-dependencies
  # Copy everything we need to run `yarn` without copying the source code so that dependencies are cached
  COPY package.json yarn.lock .yarnrc.yml redwood.toml .
  COPY .yarn .yarn
  COPY api/package.json api/package.json
  COPY web/package.json web/package.json
  COPY packages/konfig-cli/package.json packages/konfig-cli/package.json
  COPY packages/konfig-kill-port/package.json packages/konfig-kill-port/package.json
  COPY packages/konfig-lib/package.json packages/konfig-lib/package.json
  COPY packages/konfig-openapi-spec/package.json packages/konfig-openapi-spec/package.json
  COPY packages/konfig-postman-to-openapi/package.json packages/konfig-postman-to-openapi/package.json
  COPY packages/konfig-release-it/package.json packages/konfig-release-it/package.json
  COPY packages/konfig-spectral-ruleset/package.json packages/konfig-spectral-ruleset/package.json
  COPY packages/konfig-swagger2openapi/package.json packages/konfig-swagger2openapi/package.json
  COPY packages/konfig-typescript-sdk/package.json packages/konfig-typescript-sdk/package.json
  COPY packages/konfig-zod-to-openapi/package.json packages/konfig-zod-to-openapi/package.json
  RUN --secret NPM_TOKEN yarn --frozen-lockfile
  # Now that dependencies are installed, copy the source code and build
  COPY api api
  COPY web web
  COPY packages packages
  COPY bash-scripts/build.sh bash-scripts/build.sh
  RUN --secret NPM_TOKEN yarn build

run-konfig-api:
  FROM +build-konfig-dash
  ENTRYPOINT ["yarn", "rw", "deploy", "render", "api"]
  EXPOSE 8911
  SAVE IMAGE konfig-api:latest
